package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import java.text.SimpleDateFormat;
import java.util.*;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfo;
import com.itheima.pojo.Setmeal;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;

    @RequestMapping("/getOrderReport")
    public Result getMemberReport() {
        Map<String, Object> map = new HashMap<>();
        List<String> months = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();//获得日历对象,模拟时间就是当前时间
        //计算过去一年的12个月
        calendar.add(Calendar.DATE, -10);//获得当前时间往前推12个月的时间
        for (int i = 0; i < 10; i++) {
            calendar.add(Calendar.DATE, 1);//获得当前时间往后推一个月日期
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy.MM.dd").format(date));
        }
        map.put("months", months);
        List<Integer> OrderCount = orderService.findOrderByDate(months);
        List<Integer> VisitsCount = orderService.findReachByDate(months);
        map.put("OrderCount", OrderCount);
        map.put("VisitsCount", VisitsCount);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    @RequestMapping("/getDateOrder")
    public Result getDateOrder(@RequestBody Date[] dateRange) {
        Map<String, Object> map = new HashMap<>();
        List<String> months = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        long time = dateRange[1].getTime() - dateRange[0].getTime();
        Integer day = Math.toIntExact(time / (24 * 60 * 60 * 1000));
        calendar.setTime(dateRange[0]);
        calendar.add(Calendar.DATE, -1);
        for (Integer i = 0; i < day; i++) {
            calendar.add(Calendar.DATE, 1);
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy.MM.dd").format(date));
        }
        map.put("months", months);
        List<Integer> OrderCount = orderService.findOrderByDate(months);
        List<Integer> VisitsCount = orderService.findReachByDate(months);
        map.put("OrderCount", OrderCount);
        map.put("VisitsCount", VisitsCount);
        return new Result(true, "查询成功", map);
    }

    @RequestMapping("/findPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) throws Exception {
        return orderService.findByPage(queryPageBean);
    }

    @RequestMapping("/findAllSetmealName")
    public Result findAllSetmealName() {
        try {
            List<String> list = orderService.findAllSetmealName();
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }


    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Setmeal> list = orderService.findAll();
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody OrderInfo orderInfo, Integer[] setmealIds) {
        System.out.println(setmealIds.length);
        try {
            if (setmealIds.length == 0) {
                return new Result(false, "未选择预约套餐,请先选择体检套餐!");
            } else {
                orderInfo.setOrderType(Order.ORDERTYPE_TELEPHONE);
                Result result;
                result = orderService.add(orderInfo, setmealIds);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FALSE);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map<String, Object> map = new HashMap<>();
            map = orderService.findById(id);
            return new Result(true, MessageConstant.GET_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDER_FALSE);
        }
    }

    @RequestMapping("/findSetmealByOrderId")
    public Result findSetmealById(Integer id) {
        try {
            List<Integer> setmealIds = orderService.findSetmealById(id);
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Order order, Integer[] setmealIds) {
        System.out.println(setmealIds.length);
        try {
            if (setmealIds.length == 0) {
                return new Result(false, "请选择需要修改后的体检套餐");
            }
            Result result;
            result = orderService.edit(order, setmealIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ORDER_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            orderService.delete(id);
            return new Result(true, MessageConstant.DELETE_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.DELETE_ORDER_FAIL);
        }
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(Integer id) {
        try {
             orderService.updateStatus(id);
            return new Result(true, MessageConstant.UPDATE_ORDERSTATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_ORDERSTATUS_FAIL);
        }
    }
}
